{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21cfea06-af98-496a-b13b-106c335a2e65",
   "metadata": {},
   "source": [
    "# Understand Events and their Effects\n",
    "\n",
    "Effects are useful to create conditional relationships in your data: if some events happen `Effect`s can be configured to modify vectors in order to reflect the induced change of the event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==3.38.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from superlinked.framework.common.schema.event_schema import event_schema\n",
    "from superlinked.framework.common.schema.id_schema_object import IdField\n",
    "from superlinked.framework.common.schema.schema import schema\n",
    "from superlinked.framework.common.schema.schema_object import String\n",
    "from superlinked.framework.common.schema.schema_reference import SchemaReference\n",
    "from superlinked.framework.dsl.index.index import Index\n",
    "from superlinked.framework.dsl.space.text_similarity_space import TextSimilaritySpace\n",
    "from superlinked.framework.dsl.index.effect import Effect\n",
    "from superlinked.framework.dsl.query.param import Param\n",
    "\n",
    "from superlinked.framework.dsl.executor.in_memory.in_memory_executor import (\n",
    "    InMemoryExecutor,\n",
    ")\n",
    "from superlinked.framework.dsl.source.in_memory_source import InMemorySource\n",
    "from superlinked.framework.dsl.query.query import Query\n",
    "\n",
    "pd.set_option(\"display.max_colwidth\", 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70951ddc-ef9d-44af-a984-bebe69ca70e6",
   "metadata": {},
   "source": [
    "## Setting up event schemas\n",
    "\n",
    "Events generally have:\n",
    "- `SchemaReference`s: these contain ids that are resolved in the referenced schema. These reflect the items which were constituents of the event.\n",
    "- event_type as a string: used to group events so that `Effect`s can be applied to a subset of events\n",
    "- and id of course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "@schema\n",
    "class Paragraph:\n",
    "    id: IdField\n",
    "    body: String\n",
    "\n",
    "\n",
    "@schema\n",
    "class User:\n",
    "    id: IdField\n",
    "    interest: String\n",
    "\n",
    "\n",
    "@event_schema\n",
    "class Event:\n",
    "    id: IdField\n",
    "    paragraph: SchemaReference[Paragraph]\n",
    "    user: SchemaReference[User]\n",
    "    event_type: String\n",
    "\n",
    "\n",
    "paragraph = Paragraph()\n",
    "user = User()\n",
    "event = Event()\n",
    "\n",
    "relevance_space = TextSimilaritySpace(\n",
    "    text=[user.interest, paragraph.body],\n",
    "    model=\"sentence-transformers/all-mpnet-base-v2\",\n",
    ")\n",
    "index = Index(\n",
    "    spaces=relevance_space,\n",
    "    effects=[\n",
    "        Effect(\n",
    "            relevance_space,\n",
    "            event.user,\n",
    "            0.8 * event.paragraph,\n",
    "            event.event_type == \"read\",\n",
    "        )\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source_paragraph: InMemorySource = InMemorySource(paragraph)\n",
    "source_user: InMemorySource = InMemorySource(user)\n",
    "source_event: InMemorySource = InMemorySource(event)\n",
    "executor = InMemoryExecutor(\n",
    "    sources=[source_paragraph, source_user, source_event], indices=[index]\n",
    ")\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "source_paragraph.put(\n",
    "    [\n",
    "        {\"id\": \"paragraph-1\", \"body\": \"Glorious animals live in the wilderness.\"},\n",
    "        {\n",
    "            \"id\": \"paragraph-2\",\n",
    "            \"body\": \"Growing computation power enables advancements in AI.\",\n",
    "        },\n",
    "    ]\n",
    ")\n",
    "\n",
    "source_user.put([{\"id\": \"user-1\", \"interest\": \"I am interested in wild animals.\"}])\n",
    "\n",
    "source_event.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"event-1\",\n",
    "            \"paragraph\": \"paragraph-2\",\n",
    "            \"user\": \"user-1\",\n",
    "            \"event_type\": \"read\",\n",
    "        }\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "198f0bce-5d7c-412b-a989-d0ab60982770",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = Query(index).find(paragraph).with_vector(user, Param(\"user_id\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = app.query(\n",
    "    query,\n",
    "    user_id=\"user-1\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e9ade3-0f54-4a0c-88e8-85641b6a140f",
   "metadata": {},
   "source": [
    "## The power of events\n",
    "\n",
    "Even though the user expressed interest in wild animals, as he interacted with the other document about AI the preferences shifted towards the actual experienced interests."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "56c2c68b-c04a-4370-b7f8-9c5b0a9a6c2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    body           id\n",
       "0  Growing computation power enables advancements in AI.  paragraph-2\n",
       "1               Glorious animals live in the wilderness.  paragraph-1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_pandas()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "superlinked-py3.10",
   "language": "python",
   "name": "superlinked-py3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
